package com.fc.happyteam.job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;

/**
 * Create by Charles.
 * Start the process when server start. It will check if required job has been scheduled or not. If not schedule it.
 * 由于本框架数据库采用自动增量创建，在首次启动时Quartz相关表尚未创建，启动会出错。暂时注掉了这些程序。如果您需要使用Quartz
 * 任务，等首次启动成功后再放开注释即可正常使用。您也可以使用SpringBoot自带的计划任务，参考SpringScheduleTask。
 * NOTE: As the Quartz Job Configuration requires database schema. And Happyteam using auto increamental db migration.
 * The Quartz relative tables has not been creaeted yet.
 * Comment out here in case bring up server failed first time.
 * Release the comments when you want use Quartz schedule job after DB schema created by DBMigration process.
 * Alternatively, you can use SpringBoot scheduling job as well, Refer to SpringScheduleTask.
 */

public class SchedulePreProcessor implements DisposableBean {
    private static final Logger log = LoggerFactory.getLogger(SchedulePreProcessor.class);

    SchedulePreProcessor(){
//        log.info("Scheduler Job pre processor start.");
//        try{
//            if(!checkExists("QuartzSampleJob", "ht_demo")){
//                addJob("QuartzSampleJob", "ht_demo","8 8 0/1 * * ?");//every hour
//                //addJob("HkxDailySettlementJob", "ht_demo","8 0/1 * * * ?");//every minute
//                //addJob("HkxDailySettlementJob", "ht_demo","0/3 * * * * ?");
//            }
//        } catch (Exception se){
//            log.error("Scheduler Job pre processor failed.");
//        }
//        log.info("Scheduler Job pre processor complete.");
    }

    private boolean checkExists(String jobClassName, String jobGroupName) throws SchedulerException
    {
//        return new StdSchedulerFactory().getScheduler().checkExists(JobKey.jobKey(jobClassName, jobGroupName));
        return false;
    }

    private void addJob(String jobClassName, String jobGroupName, String cronExpression)throws Exception{
//
//        SchedulerFactory sf = new StdSchedulerFactory();
//        Scheduler scheduler = sf.getScheduler();
//
//        // 启动调度器
//        scheduler.start();
//
//        //构建job信息
//        JobDetail jobDetail = JobBuilder.newJob(QuartzSampleJob.class).withIdentity(jobClassName, jobGroupName).build();
//
//        //表达式调度构建器(即任务执行的时间)
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//
//        //按新的cronExpression表达式构建一个新的trigger
//        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
//                .withSchedule(scheduleBuilder).build();
//
//        try {
//            scheduler.scheduleJob(jobDetail, trigger);
//
//        } catch (SchedulerException e) {
//            log.error("创建定时任务失败"+e);
//            throw new Exception("创建定时任务失败");
//        }
    }


//    public static void jobdelete(String jobClassName, String jobGroupName) throws Exception
//    {
//        SchedulerFactory sf = new StdSchedulerFactory();
//        Scheduler scheduler = sf.getScheduler();
//
//        scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
//        scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
//        scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
//    }

    @Override
    public void destroy(){
    }
}
